//
//  YDate.m
//  YuiKe
//
//  Created by yuike6 yuike6 on 11-10-11.
//  Copyright 2011年 yuike. All rights reserved.
//

#import "NSDate+Extension.h"

@implementation NSDate (NSDateExtension)


+ (NSString *) currectDay
{  
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"%@", strDate);
    [dateFormatter release];
    return strDate;
}

+ (NSString *) date2string:(NSDate *) _date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:_date];
    NSLog(@"%@", strDate);
    [dateFormatter release];
    return strDate;
}

+ (NSDate *) string2date:(NSString *) _string
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:_string];
    NSLog(@"%@", date);
    [dateFormatter release];
    
    return date;
}

/*
 返回当前周的开始日期
 */

- (NSDate *)beginOfWeek {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];    
    
    NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];
    
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];    
    // to get the end of week for a particular date, add ( -weekday) days
    
    [componentsToAdd setDay:( -[weekdayComponents weekday])];    
    NSDate *beginOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    [componentsToAdd release];
    
    return beginOfWeek;    
}

/*
    返回当前周的周末
 */

- (NSDate *)endOfWeek {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];    
    
    NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];
    
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];    
    // to get the end of week for a particular date, add (7 - weekday) days
    
    [componentsToAdd setDay:(7 - [weekdayComponents weekday])];    
    NSDate *endOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    [componentsToAdd release];
        
    return endOfWeek;    
}

- (NSUInteger)getDay{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *dayComponents = [calendar components:(NSDayCalendarUnit) fromDate:self];
    
    return [dayComponents day];    
}

//获取月

- (NSUInteger)getMonth
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];    
    NSDateComponents *dayComponents = [calendar components:(NSMonthCalendarUnit) fromDate:self];
    
    return [dayComponents month];    
}

//获取年
- (NSUInteger)getYear
{    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *dayComponents = [calendar components:(NSYearCalendarUnit) fromDate:self];    
    return [dayComponents year];    
}


/*
    month个月后的日期
 */

- (NSDate *)dateafterMonth:(int)month
{    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    
    [componentsToAdd setMonth:month];
    
    NSDate *dateAfterMonth = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    [componentsToAdd release];
    return dateAfterMonth;    
}



/*
 返回hour 小时后的日期(若hour为负数,则为|hour|小时前的日期)
 */
- (NSDate *)dateAfterHour:(int)hour
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    
    // to get the end of week for a particular date, add (7 - weekday) days
    
    [componentsToAdd setHour:hour];
    
    NSDate *dateAfterDay = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    [componentsToAdd release];    
    return dateAfterDay;

}

/*
    返回day天后的日期(若day为负数,则为|day|天前的日期)
 */

- (NSDate *)dateAfterDay:(int)day
{    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    
    // to get the end of week for a particular date, add (7 - weekday) days
    
    [componentsToAdd setDay:day];
    
    NSDate *dateAfterDay = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    [componentsToAdd release];    
    return dateAfterDay;
    
}

/*
    该月的第一天
 */
- (NSDate *)beginningOfMonth
{    
    return [self dateAfterDay:-[self getDay] + 1];    
}


/*
    该月的最后一天
 */
- (NSDate *)endOfMonth
{    
    return [[[self beginningOfMonth] dateafterMonth:1] dateAfterDay:-1];    
}

//该日期是该年的第几周
- (int )getWeekOfYear
{    
    int i;
    
    int year = [self getYear];
    
    NSDate *date = [self endOfWeek];
    
    for (i = 1;[[date dateAfterDay:-7 * i] getYear] == year;i++) 
        
    {
        
    }    
    return i;    
}



+ (NSString *) time2Now:(NSTimeInterval) time
{
    NSString * result = nil;
    
    if (time < 60) 
    {
        result = [NSString stringWithFormat:@"%.f秒", time];
    }
    else if (time < 60 * 60)
    {
        int min = (float)time / (float)(60);
        
        if (min >= 25 && 
            min <= 59) 
        {
            result = [NSString stringWithString:@"半小时"];
        }
        else 
        {
            result = [NSString stringWithFormat:@"%d分钟",  min];
        }        
    }
    else if (time < 60 * 60 * 24)
    {
        int hour = (float)time / (float)(60 * 60);
        
        if (hour >= 5 &&
            hour <= 11) 
        {
            result = [NSString stringWithString:@"半天"];
        }
        if (hour >= 12 &&
            hour <= 23) 
        {
            result = [NSString stringWithString:@"半天"];
        }
        else 
        {
            result = [NSString stringWithFormat:@"%d小时",  hour];
        }        
    }
    else if (time < 60 * 60 * 60 * 24)
    {
        int day = (float)time / (float)(60 * 60 * 24);
        
        if (day >= 7 &&
            day <= 13) 
        {
            result = [NSString stringWithString:@"一周"];
        }
        else if (day >= 14 &&
                 day <= 28)
        {
            result = [NSString stringWithString:@"半月"];
        }
        else if (day >= 29 &&
                 day <= 58)
        {
            result = [NSString stringWithString:@"一月"];
        }
        else if (day >= 59 &&
                 day <= 88)
        {
            result = [NSString stringWithString:@"二月"];
        }
        else if (day >= 89 &&
                 day <= 118)
        {
            result = [NSString stringWithString:@"三月"];
        }
        else if (day >= 119 &&
                 day <= 148)
        {
            result = [NSString stringWithString:@"四月"];
        }
        else if (day >= 149 &&
                 day <= 178)
        {
            result = [NSString stringWithString:@"五月"];
        }
        else if (day >= 179 &&
                 day <= 208)
        {
            result = [NSString stringWithString:@"半年"];
        }
        else if (day >= 209 &&
                 day <= 238)
        {
            result = [NSString stringWithString:@"七月"];
        }
        else if (day >= 239 &&
                 day <= 268)
        {
            result = [NSString stringWithString:@"八月"];
        }
        else if (day >= 269 &&
                 day <= 298)
        {
            result = [NSString stringWithString:@"九月"];
        }
        else if (day >= 299 &&
                 day <= 318)
        {
            result = [NSString stringWithString:@"十月"];
        }
        else if (day >= 329 &&
                 day <= 348)
        {
            result = [NSString stringWithString:@"十一月"];
        }
        else if (day >= 349 /*&&
                 day <= 380*/)
        {
            result = [NSString stringWithString:@"一年"];
        }
        else 
        {
            result = [NSString stringWithFormat:@"%d天",  day];
        }        
    }
    
    return result;
}

@end
